/**
 * 资产标签 列表页 JS 脚本
 * @author 金杰 , maillank@qq.com
 * @since 2022-05-20 20:59:20
 */


function ListPage() {

	var settings,admin,form,table,layer,util,fox,upload,xmSelect;
	//模块基础路径
	const moduleURL="/service-eam/eam-asset-label";
	var dataTable=null;
	var sort=null;
	var cardArr=[];
	var curTplId="";
	var labelData={};
	/**
      * 入口函数，初始化
      */
	this.init=function(layui) {

     	admin = layui.admin,settings = layui.settings,form = layui.form,upload = layui.upload,laydate= layui.laydate;
		table = layui.table,layer = layui.layer,util = layui.util,fox = layui.foxnic,xmSelect = layui.xmSelect,dropdown=layui.dropdown;;

		if(window.pageExt.list.beforeInit) {
			window.pageExt.list.beforeInit();
		}
     	//渲染表格
     	renderTable();
		//初始化搜索输入框组件
		initSearchFields();
		//绑定搜索框事件
		bindSearchEvent();
		//绑定按钮事件
		bindButtonEvent();
		//绑定行操作按钮事件
    	bindRowOperationEvent();
     }


	$("#cust").click(function(){

		// alert("未正确配置");
		// return 1
		var queryString=""
		var index=admin.popupCenter({
			title: "自定义标签",
			resize: false,
			offset: [15,null],
			area: ["80%","98%"],
			type: 2,
			id:"eam-asset-label-custom-win",
			content: '/business/eam/asset_label/asset_label_custom.html' + queryString,
			finish: function () {
				renderTable();
			}
		});
		admin.putTempData('eam-asset-label-custom-popup-index', index);
	});







	function renderLabelHtml(obj){
		console.log("renderLabelHtml",obj);
		var selClass="area-hide";
		var delClass="";
		if(obj.isCustom=="0"){
			delClass="area-hide";
		}

//////////////////////////////////////////////////////////////////////
		var txmPic="";
		var rwmPic="";
		var pic;
		var picAddCss=""
		if(obj.imageShow&&obj.imageShow=="1"){
			if(obj.imageType=="txm"){
				pic=txmPic;
			}else{
				pic=rwmPic;
				picAddCss="style=\"width:80px;height:80px\"";
			}
		}
		var picHtml="";
		picHtml="                         <tr>\n" +
			"                                        <td colspan=\"2\" class=\"text-center\">\n" +
			"                                         <img class=\"barcode-style\" "+picAddCss+" src=\""+pic+"\">"
		picHtml=picHtml+"									 <div class=\"img-bottom-barcode\">barcode</div>\n"

		picHtml=picHtml+"                               </td>\n" +
			"                                    </tr>\n"
//////////////////////////////////////////////////////////////////////

		var trListHtml=""

		var html="   <div onclick=\"event.stopPropagation();cardClick('"+obj.id+"')\" id=\"cd"+obj.id+"\" class=\"op-block showCard relative\">\n" +
			"                        <div style=\"border: 1px solid #000;\">\n" +
			"                            <table cellspacing=\"0\" >\n" +
			"                                <tbody>\n"
		//填充上面图片
		if(obj.imagePosition=="u"){
			html=html+picHtml
		}

		//填充中间图片
		var list=obj.assetLabelColumnlList;
		if(list&&list.length>0){
			for(var i=0;i<list.length;i++){
				var col=list[i];
				var label=""
				if(col&&col.colName){
					label=col.colName;
				}
				trListHtml=trListHtml+"            <tr>\n" +
					"                                        <td width=\"30\">"+label+"</td>\n" +
					"                                        <td width=\"70\">XXXXXX</td>\n" +
					"                                    </tr>\n";
			}
		}
		html=html+trListHtml
		//填充下方图片
		if(obj.imagePosition=="d"){
			html=html+picHtml
		}

		html=html+"                           </tbody>\n" +
			"                            </table>\n" +
			"                        </div>\n" +
			"                        <div id=\"cdSel"+obj.id+"\" class=\""+selClass+"\">\n" +
			"                            <div class=\"selected-point color-primary \"><i class=\"fa fa-check-circle\"></i></div>\n" +
			"                        </div>\n" +
			"                        <a class=\"cursor-pointer "+delClass+"\" onclick=\"event.stopPropagation();deleteLabel('"+obj.id+"')\">\n" +
			"                            <div class=\"absolute delete-custom-button-style\"><i class=\"fa fa-times\"></i></div>\n" +
			"                        </a>\n" +
			"                    </div>"

		return html;
	}

	window.cardClick=function(value){
		for(var i=0;i<cardArr.length;i++){
			$("#cdSel"+cardArr[i].id).removeClass("area-hide");
			$("#cdSel"+cardArr[i].id).addClass("area-hide");
		}
		curTplId=value;
		$("#cdSel"+value).removeClass("area-hide");

	}

	window.deleteLabel=function(value){
		console.log(value);
		var api="/service-eam/eam-asset-label-tpl/delete";
		top.layer.confirm(fox.translate('确定进行删除本模版吗？'), function (i) {
			top.layer.close(i);
			admin.post(api, {id:value}, function (r) {
				if (r.success) {
					top.layer.msg("操作成功", {time: 1000});
					renderTable();
				} else {
					var errs = [];
					if (r.errors) {
						for (var i = 0; i < r.errors.length; i++) {
							if (errs.indexOf(r.errors[i].message) == -1) {
								errs.push(r.errors[i].message);
							}
						}
						top.layer.msg(errs.join("<br>"), {time: 2000});
					} else {
						top.layer.msg(r.message, {time: 2000});
					}
				}
			}, {delayLoading: 1000, elms: []});
		});


	}


	/**
      * 渲染表格
      */
    function renderTable() {


		admin.post("/service-eam/eam-asset-label-tpl/query-list", {}, function (data) {
			if (data.success) {
				cardArr=data.data;
				var html="";
				for(var i=0;i<cardArr.length;i++){
					var h="<div class=\"card\" style=\"margin-left: 0%; width: 33.3333%;\">"
					var card=renderLabelHtml(cardArr[i]);
					h=h+card+"</div>";
					html=html+h;
				}
				$("#cardList").html(html);

				admin.post("/service-eam/eam-asset-label/query-asset-label", {}, function (res) {
					if (res.success) {
						labelData=res.data;
						$("#labelWidth").val(labelData.labelWidth);
						$("#labelHeight").val(labelData.labelHeight);
						var radio = document.getElementsByName("paperTypeId");
						var radioLength = radio.length;
						for (var i = 0; i < radioLength; i++) {
							if(labelData.paperTypeId == radio[i].value){
								$(radio[i]).next().click();
							}
						}
						if(labelData.labelTplId){
							curTplId=labelData.labelTplId;
							$("#cdSel"+labelData.labelTplId).removeClass("area-hide");
						}
					} else {
						fox.showMessage(res);
					}
				}, {delayLoading:1000,elms:[]});



			} else {
				fox.showMessage(data);
			}

		}, {delayLoading:1000,elms:[]});




    };

	/**
	 * 刷新单号数据
	 * */
	function refreshRowData(data,remote) {

	}

	/**
      * 刷新表格数据
      */
	function refreshTableData(sortField,sortType,reset) {

	}


	/**
	  * 获得已经选中行的数据,不传入 field 时，返回所有选中的记录，指定 field 时 返回指定的字段集合
	  */
	function getCheckedList(field) {

	}

	/**
	 * 重置搜索框
	 */
	function resetSearchFields() {

	}

	function initSearchFields() {

	}

	/**
	 * 绑定搜索框事件
	 */
	function bindSearchEvent() {

	}

	/**
	 * 绑定按钮事件
	  */
	function bindButtonEvent() {
		form.on('submit(submit-button)', function (data) {
			console.log(data);
			 var ps=data.field;
			 ps.labelTplId=curTplId;
			var api="/service-eam/eam-asset-label/save-asset-label";
			top.layer.confirm(fox.translate('是否保存当前资产标签配置？'), function (i) {
				top.layer.close(i);
				admin.post(api, ps, function (r) {
					if (r.success) {
						top.layer.msg("操作成功", {time: 1000});
					} else {
						top.layer.msg(r.message, {time: 1000});
					}
				}, {delayLoading: 1000, elms: []});
			});
			 return false;
		});

	}

    /**
     * 绑定行操作按钮事件
     */
    function bindRowOperationEvent() {

    };

    /**
     * 打开编辑窗口
     */
	function showEditForm(data) {

	};

	window.module={
		refreshTableData: refreshTableData,
		refreshRowData: refreshRowData,
		getCheckedList: getCheckedList
	};

	window.pageExt.list.ending && window.pageExt.list.ending();

};


layui.use(['form', 'table', 'util', 'settings', 'admin', 'upload','foxnic','xmSelect','laydate','dropdown'],function() {
	var task=setInterval(function (){
		if(!window["pageExt"]) return;
		clearInterval(task);
		(new ListPage()).init(layui);
	},1);
});